package com.icode.core.shared;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-4
 * Time: 上午11:33
 * To change this template use File | Settings | File Templates.
 */
public class HttpSend {


    public static String postSend(String strUrl,String param){

        URL url = null;
        HttpURLConnection connection = null;

        try {
            url = new URL(strUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST"); //get post head options put delete trace 以上方法之一是合法的
            connection.setUseCaches(false);  //是否使用缓存
            connection.connect();

            //POST方法时使用
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(param);
            out.flush();
            out.close();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
    /**
     * 转为16进制方法
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String paraTo16(String str) {
        String hs = "";
        try {
            byte[] byStr = new byte[0];
            byStr = str.getBytes("UTF-8");
            for(int i=0;i<byStr.length;i++)
            {
                String temp = "";
                temp = (Integer.toHexString(byStr[i]&0xFF));
                if(temp.length()==1) temp = "%0"+temp;
                else temp = "%"+temp;
                hs = hs+temp;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return hs.toUpperCase();
    }

}
